/*
 * create:2016年3月28日 下午5:59:40
 * author:wangds  
 */
package com.cxkj.shop.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cxkj.shop.utils.WxUtils;
import com.cxkj.shop.utils.WxXmlUtils;

import net.wangds.common.GeneralParentObject;
import net.wangds.precasting.processor.io.InputStreamReadAsStringProcessor;
import net.wangds.utilities.lang.StringUtils;

/**
 * 配置时.
 * @author wangds
 *
 */
public class ReceiveTokenServlet extends HttpServlet{

  /**
   * ReceiveTokenServlet.java.
   * @author wangds 2016年3月28日 下午6:00:10.
   */
  private static final long serialVersionUID = 2220395004948277804L;

  /*
   * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   * @author wangds 2016年3月28日 下午6:00:23.
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String signature = req.getParameter("signature");
    String timestamp = req.getParameter("timestamp");
    String nonce = req.getParameter("nonce");
    String echostr = req.getParameter("echostr");
    //http://wangds.net:8080/wx/servlet/receiveToken?signature=1&timestamp=2&nonce=3&echostr=4
    GeneralParentObject.staticLogger(this.getClass()).info("signature:"+signature+";timestamp:"+timestamp+";nonce:"+nonce+";echostr:"+echostr);
    
    ServletOutputStream out = resp.getOutputStream();
    out.write(echostr.getBytes());
    out.flush();
    
  }
  
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //String signature = req.getParameter("signature");
    //String timestamp = req.getParameter("timestamp");
    //String nonce = req.getParameter("nonce");
    
    //System.out.println("signature:"+signature+";timestamp:"+timestamp+";nonce:"+nonce+";");
    String req_xml_str = readStringFromPostReqBody(req);
    GeneralParentObject.staticLogger(this.getClass()).info("receive:"+req_xml_str);
    String dev = WxXmlUtils.cutXmlValue(req_xml_str, "ToUserName");
    String cus = WxXmlUtils.cutXmlValue(req_xml_str, "FromUserName");
    //String key = WxXmlUtils.cutXmlValue(req_xml_str, "EventKey");
    
    String resp_xml_str=StringUtils.join( 
    "<xml>",
    "<ToUserName><![CDATA[",cus,"]]></ToUserName>",
    "<FromUserName><![CDATA[",dev,"]]></FromUserName>",
    "<CreateTime>",System.currentTimeMillis()/1000,"</CreateTime>",
    "<MsgType><![CDATA[text]]></MsgType>",
    "<Content><![CDATA[",WxUtils.welcomeMsg(),"]]></Content>",
    "</xml>"
    );
    
    
    ServletOutputStream out = resp.getOutputStream();
    out.write(resp_xml_str.getBytes("UTF8"));
    out.flush();
    
  }
  
  private static String readStringFromPostReqBody(HttpServletRequest req) throws IOException{
    ServletInputStream is = req.getInputStream();
    InputStreamReadAsStringProcessor<InputStream> proc = new InputStreamReadAsStringProcessor<>();
    proc.setInputStream(is);
    proc.setEncoding(req.getCharacterEncoding());
    proc.process();
    return proc.getResult();
  }
}
